package ua.keeper.query;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
    private SQLiteDatabase db_;
    private TPList tpListDB_;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
    	Log.w("SettingsActivity", "onCreate");
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
        
    	//DbOpenHelper dbOpenHelper = new DbOpenHelper(SettingsActivity.this);
        //db_ = dbOpenHelper.getWritableDatabase();
        QueryApp appState = ((QueryApp)getApplicationContext());
        db_ = appState.getDataBase();
        
        loadPreferences();
    }
    /*****************************************************************************************/
    @Override
    protected void onStop()
    {
    	//db_.close();
    	super.onStop();
    }
    /*****************************************************************************************/
    /*Load Preferences*/
    protected void loadPreferences()
    {
    	// TP list
    	//tpListDB_ = new TPList(db_);
    	//ListPreference tpList = (ListPreference) findPreference("tpList");
    	//tpList.setEntries(tpListDB_.getTitleArray());
        //tpList.setEntryValues(tpListDB_.getIdArray());
        //tpList.setSummary(tpListDB_.getTitleById(tpList.getValue())); 
        
        // Server Name
        EditTextPreference refServer = (EditTextPreference) findPreference("serverPort");
        refServer.setSummary(refServer.getText());
        
        // Server Port
        EditTextPreference refPort = (EditTextPreference) findPreference("serverPort");
        refPort.setSummary(refPort.getText()); 
        
        // Device-ID
        Preference devId = (Preference) findPreference("device_id");
        devId.setSummary(Settings.Secure.ANDROID_ID);
    }
    @Override
    protected void onResume()
    {
        super.onResume();

        // Setup the initial values

        // Set up a listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener((OnSharedPreferenceChangeListener) this);
    }

    /*****************************************************************************************/
    @Override
    protected void onPause()
    {
        super.onPause();
        // Unregister the listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener((OnSharedPreferenceChangeListener) this);    
    }
    /*****************************************************************************************/
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        // preference value changes
        if (key.equals("tpList"))
        {
        	Toast.makeText(this, "<УБРАТЬ - лишнее>", Toast.LENGTH_SHORT).show();
        	//ListPreference ref = (ListPreference) findPreference("tpList");
        	//ref.setSummary(tpListDB_.getTitleById(ref.getValue())); 
        }
        if (key.equals("serverPort"))
        {
        	EditTextPreference ref = (EditTextPreference) findPreference("serverPort");
        	ref.setSummary(ref.getText()); 
        }
        if (key.equals("serverName"))
        {
        	EditTextPreference ref = (EditTextPreference) findPreference("serverName");
        	ref.setSummary(ref.getText()); 
        }
    }
}
